<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>淘有货后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <%--<link rel="shortcut icon" href="${pageContext.request.contextPath}/font/favicon.ico" type="image/x-icon"/>--%>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main/xadmin.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/swiper.min.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/login/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/swiper.jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/main/xadmin.js"></script>

</head>
<body>
<!-- 中部开始 -->
<div class="wrapper">
    <!-- 右侧主体开始 -->
    <div class="page-content">
        <div class="">
            <!-- 右侧内容框架，更改从这里开始 -->
            <c:if test="${type == 'product'}">
                <form id="touch" class="layui-form">
                    <input type="text" value="${product.pid}" id="pid" name="pid" hidden="hidden">
                    <div class="layui-form-item">
                        <label for="name" class="layui-form-label">
                            <span class="x-red">*</span>商品名
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" required="" value="${product.name}" lay-verify="name"
                                   autocomplete="off" class="layui-input">
                        </div>
                        <!-- <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您唯一的登入名
                </div> -->
                    </div>
                    <div class="layui-form-item" style="height: 100px;">
                        <label for="image" class="layui-form-label">
                            <span class="x-red">*</span>商品图片
                        </label>

                        <div class="layui-input-inline" style="width: 70px">
                            <input type="file" id="file" name="file" required="" accept="image/jpg,image/png" autocomplete="off" hidden>
                            <input type="button" class="layui-btn layui-btn-normal layui-btn-small" value="更换图片"
                                   onclick="fileclick()">
                            <!-- 	<button type="button" class="layui-btn" id="test1">
                                    <i class="layui-icon">&#xe67c;</i>上传图片
                                </button> -->
                        </div>

                        <div class="layui-input-inline">
                            <img id="image" src="/client/${product.imgs}" alt="${product.name}"
                                 style="width: 70px;height: 70px;position: absolute;float: right;top:-5px;">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="cid" class="layui-form-label">
                            <span class="x-red">*</span>商品分类
                        </label>
                        <div class="" style="width: 47%;display: inline-block">
                            <select name="cid" id="cid">
                                <c:forEach items="${list}" var="item">
                                    <<option value="${item.cid}">${item.cname}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="marketPrice" class="layui-form-label">
                            <span class="x-red">*</span>商品原价
                        </label>
                        <div class="layui-input-inline">
                            <input type="number" id="marketPrice" name="marketPrice" required=""
                                   value="${product.marketPrice}" lay-verify="pass" autocomplete="off"
                                   class="layui-input">
                        </div>
                        <!-- <div class="layui-form-mid layui-word-aux">
                    6到16个字符
                </div> -->
                    </div>
                    <div class="layui-form-item">
                        <label for="shelfPrice" class="layui-form-label">
                            <span class="x-red">*</span>活动价格
                        </label>
                        <div class="layui-input-inline">
                            <input type="number" id="shelfPrice" name="shelfPrice" required=""
                                   value="${product.shelfPrice}" lay-verify="shelfPrice"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="detail" class="layui-form-label">
                            商品描述
                        </label>
                        <div class="layui-input-inline">
                        <textarea id="detail" name="detail" required="" placeholder="" rows="5" cols="30" lay-verify="detail"
                                  autocomplete="off" class="layui-textarea">${product.detail}</textarea>
                                <%--<textarea id="desc" name="desc" placeholder="请输入内容"  class="layui-textarea">${product.desc}</textarea>--%>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="editProduct" class="layui-form-label">
                        </label>
                        <input type="button" value="修改" id="editProduct" class="layui-btn" lay-filter="add" lay-submit="">

                        </input>
                    </div>
                </form>
            </c:if>
            <c:if test="${type == 'stock'}">
                <form id="touch_stock">
                    <div class="layui-form-item">
                        <label for="stock" class="layui-form-label">
                            <span class="x-red">*</span>库存
                        </label>
                        <div class="layui-input-inline">
                            <input type="number" id="stock" name="stock" required="" min="0" max="20000"
                                   value="${product.stock}" lay-verify="stock"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <input type="button" value="修改"  onclick="edit_Stock('${product.pid}')" class="layui-btn" style="float: right"/>
                </form>
            </c:if>
            <!-- 右侧内容框架，更改从这里结束 -->
        </div>
    </div>
    <!-- 右侧主体结束 -->
</div>
<!-- 中部结束 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/api/adminStore.js"></script>

<script>
    //百度统计可去掉
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>

<script type="text/javascript">
    img = "";
    $("input[type='file']").change(function () {
        var file = this.files[0];
        if (window.FileReader) {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            //监听文件读取结束后事件
            reader.onloadend = function (e) {
                console.log(reader.result);
                img = getBase64Image(reader.result);
                $("#image").attr("src", reader.result); //e.target.result就是最后的路径地址
            };
        }
    });

    getBase64Image = function (imgElem) {
        // return imgElem.replace("/^data:image\/(jpeg|jpg);base64,/", "");
        return imgElem.split(",")[1];
    }

    $("#editProduct").click(function () {
        var dataform = new FormData(document.getElementById("touch"));
        console.log(dataform);
        dataform.append("image",img);
        var name = $("#name").val();
        if(inspect(name)){
            $.ajax({
                type: "post",
                url: adminStore.context + "/product/editProduct.do",
                //                下面内容未完成
                data: dataform,
                processData: false,
                contentType: false,
                success: function (s) {
                    if (s) {
                        layer.msg('商品已修改!', {
                            icon: 1,
                            time: 1000
                        });
                        <%--window.location.href = "${pageContext.request.contextPath}/product.do";--%>
                        x_admin_close();
                        top.location.reload();
                    } else {
                        alert(s.msg);
                    }
                }
            })
        }
    });

    inspect = function (name) {
        var rule = /[a-zA-Z0-9_\u4e00-\u9fa5]+/;
        if(rule.test(name)){
            return true;
        }else{
            return false;
        }
    }

    fileclick = function () {
        $("input[name='file']").click();
    }
</script>
<script>
    edit_Stock = function (pid) {
        console.log(pid);
        var stock = $("#stock").val();
        console.log(stock)
        $.ajax({
            url:adminStore.context +"/product/editStock.do",
            type:"post",
            data:{
                pid :pid,
                stock : stock
            },
            success:function () {
                x_admin_close()
                top.location.reload();
            }
        })
    }

    $("#stock").keyup(function () {
        var _this = $(this);
        var i = _this.val();
        var norm = /^[0-9]*$/;
        if (_this.val() == "") {
            _this.val(1)
        }
        if (_this.val() < 0) {
            _this.val(0);
        }
        if (_this.val() > 20000) {
            _this.val(20000)
        }
    })
</script>
</html>
